<template>
    <el-form ref="form" v-loading="loading" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="品牌名" prop="brandName">
            <el-input v-model="form.brandName" placeholder="请输入品牌名"></el-input>
        </el-form-item>
        <el-form-item label="品牌描述" prop="introduce">
            <el-input v-model="form.introduce" placeholder="请输入品牌描述"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
            <el-input v-model="form.sort" placeholder="请输入整数"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit('form')">添加</el-button>
            <el-button>取消</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    export default {
        name: "BrandTypeAddView",
        data() {
            // 排序号只能是整数数字
            var validateSort = (rule, value, callback) => {
                let numSort = Number(value);
                if (value.length === 0){
                    callback(new Error('请输入排序号'));
                }
                if (isNaN(numSort) || !(numSort === parseInt(value))){
                    callback(new Error('只能是整数数字'));
                }
                if (numSort === parseInt(value)){
                    callback();
                }
            };
            return {
                loading: false,
                form: {
                    brandName: '',
                    introduce: '',
                    sort:''
                },
                rules: {
                    brandName: [
                        {required: true, message: '品牌名不能为空', trigger: 'blur'},
                    ],
                    introduce: [
                        {required: true, message: '品牌名描述不能为空', trigger: 'blur'},
                    ],
                    sort: [{required: true, validator: validateSort, trigger: 'blur' }],
                },
            }
        },
        methods: {
            onSubmit(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.loading = true;
                        axios.post ('/manage/brandType/add',this.form)
                            .then(response => {
                                this.loading = false;
                                let data = response.data;
                                // 添加失败
                                if (data.code!== 200) {
                                    this.$message.error(data.msg);
                                    return;
                                }
                                // 添加成功
                                this.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                // 跳转页面
                                this.$router.push({
                                    path: '/brand/list'
                                });
                            })
                            .catch(error => {
                                this.loading = false;
                                console.log(error)
                                this.$message.error('系统异常，请联系管理员或稍后再试');
                            })
                            .finally(() => this.loading = false);
                    } else {
                        this.loading = false;
                        return false;
                    }
                });
            }
        }
    }
</script>

<style scoped>

</style>